package sendmail
import (
	//"net/smtp"
	//"strings"
	"fmt"
	"gopkg.in/gomail.v2"
)

func SendEmail(contents string, target string) error {
	//auth := smtp.PlainAuth("", "1134403355@qq.com", "xnjyekceyrdd"/*填写账户授权码*/, "smtp.qq.com")
	//to := []string{target}
	//user := "1134403355@qq.com"
	//subject := "Go Mail"
	//content_type := "Content-Type: text/plain; charset=UTF-8"
	//body := "您的验证码为：" + contents + "\r\n" + "请勿告诉他人，如非本人操作请忽略本条邮件。请勿回复。"
	//msg := []byte("To: " + strings.Join(to, ",") + "\r\nFrom: " + "Web注册验证码消息" +
	//	"<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
	//
	//err := smtp.SendMail("smtp.qq.com:465", auth, user, to, msg)
	//if err != nil {
	//	fmt.Printf("邮件发送失败: %v/n", err)
	//}
	//
	//return err


	m := gomail.NewMessage()
	m.SetAddressHeader("From", "1134403355@qq.com", "Web注册验证码消息")  // 发件人
	m.SetHeader("To",  // 收件人
		m.FormatAddress(target, "111"),
	)
	m.SetHeader("Subject", "Go Mail")  // 主题
	body := "您的验证码为：" + contents + "<br>" + "请勿告诉他人，如非本人操作请忽略本条邮件。请勿回复。"
	m.SetBody("text/html", body)  // 正文

	d := gomail.NewPlainDialer("smtp.qq.com", 465, "1134403355@qq.com", "xnjyekceyrdd")  // 发送邮件服务器、端口、发件人账号、发件人密码

	err := d.DialAndSend(m)
	if err != nil {
		fmt.Printf("邮件发送失败: %v/n", err)
	}

	return err
}
